const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [
      // '全部', '待付款', '待发货', '待收货', '已完成'
      {
        id:'',
        title:'全部'
      },
      {
        id:'1',
        title:'待付款'
      },
      {
        id:'2',
        title:'待发货'
      },
      {
        id:'3',
        title:'待收货'
      },
      {
        id:'4',
        title:'已完成'
      }
  ],
    id: 0,
    lists:[],
    page:1,
    flag:true
  },
  onLoad: function (options) {
    this.setData({
      id:options.index
    })
    this.data.id = options.index
    this.getInfo(this.data.id )
  },
  onShow: function () {
    this.getInfo();
  },
  // 上拉加载
  onReachBottom: function() {
    console.log('fff')
    var that = this;
    let token = wx.getStorageSync('token')
    this.data.page = this.data.page+1
      //显示加载动画
      wx.showLoading({
        title:"刷新中",
        mask:true
      })

      app.util.request({
        url: app.do('index/OrderLists'),
        method: 'Post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          token: token,
          status: that.data.id,
          page:that.data.page
        },
        success: function (res) {
          wx.hideLoading()//关闭加载动画
          if (res.data.code == "0000") {
            //  es6解构
              that.data.lists=[...that.data.lists,...res.data.data.list]
              that.setData({
                lists:that.data.lists
              })
          } 
          else if (res.data.code == "2001") {
            wx.navigateTo({
              url: '../auth/auth',
            })
          }
        },
        fail: function (res) {
          wx.hideLoading({})
          var text = res.data.message;
          wx.showToast({
            title: text,
            icon: 'none',
            duration: 2000,
          })
        }
      })
	},
  
  getInfo() {
    let token = wx.getStorageSync('token')
    var that = this;
    that.data.page = 1;
    wx.showLoading();
    app.util.request({
      url: app.do('index/OrderLists'),
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: token,
        status: that.data.id,
        page:that.data.page
      },
      success: function (res) {
        wx.hideLoading()
        if (res.data.code == "0000") {
          that.setData({
            lists:res.data.data.list
          })
        } else if (res.data.code == "2001") {
          wx.navigateTo({
            url: '../auth/auth',
          })
        } else {
          var text = res.data.message;
          wx.showToast({
            title: text,
            icon: 'none',
            duration: 2000,
          })
        }
      },
      fail: function (res) {
        wx.hideLoading()
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  //支付
  paymentBtn(e){
    let token = wx.getStorageSync('token')
    var that = this;
    console.log('e',e.currentTarget.dataset.id)
    wx.showModal({
      title: '',
      content: '确定要支付吗？',
      success (res) {
        if (res.confirm) {
          app.util.request({
            url: app.do('index/payment'),
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              token: token,
              id: e.currentTarget.dataset.id
            },
            success: function(res) {
              if(res.data.code == "0000"){
                  let jsdk = res.data.data.jsdk;
                  //调用微信支付
                  that.pay(jsdk);
              }
              else if(res.data.code == "2001"){
                wx.navigateTo({
                  url: '../auth/auth',
                })
              }
              else{
                var text = res.data.message;
                wx.showToast({
                  title: text,
                  icon: 'none',
                  duration: 2000,
                })
              }
            },
            fail: function(res) {
              var text = res.data.message;
              wx.showToast({
                title: text,
                icon: 'none',
                duration: 2000,
              })
            }
          })
        }
        else if (res.cancel) { }
      }
    })
  },
  pay(jsdk){
    wx.requestPayment({
      nonceStr: jsdk.nonceStr,
      package: jsdk.package,
      paySign: jsdk.paySign,
      signType: 'MD5',
      timeStamp: jsdk.timeStamp,
      success (res) { 
        wx.navigateTo({
          url: '../orderList/orderList',
        })
      },
      fail (res) { }
    })
  },
  toggle(e) {
    this.setData({
      flag:true
    })
    this.setData({
      id: e.currentTarget.dataset.index
    });
    this.getInfo();
  },
  copy(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.no,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      },
      fail(err) {
      }
    })
  },
  refirm(e){
   
    let token = wx.getStorageSync('token')
    var that = this;
    wx.showModal({
      title: '',
      content: '是否确认收货？',
      success (res) {
        if (res.confirm) {
         
    that.data.page = 1;
    wx.showLoading();
    app.util.request({
      url: app.do('index/OrderConfirm'),
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: token,
        order_id: e.currentTarget.dataset.id
      },
      success: function (res) {
        wx.hideLoading()
        if (res.data.code == "0000") {
          wx.showToast({
            title: '确认收货成功',
          });
          setTimeout(() => {
            that.getInfo();
          }, 2000);
         
        } else if (res.data.code == "2001") {
          wx.navigateTo({
            url: '../auth/auth',
          })
        } else {
          var text = res.data.message;
          wx.showToast({
            title: text,
            icon: 'none',
            duration: 2000,
          })
        }
      },
      fail: function (res) {
        wx.hideLoading()
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
        } else if (res.cancel) {
        }
      }
    })

  },
  toDetail(e) {
    wx.navigateTo({
      url: '../orderDetail/orderDetail?id='+e.currentTarget.dataset.id,
    })
  },
})